﻿using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
using System;
using System.IO;

    public class GameMapBlockManager
    {
       #region
       static GameMapBlockManager _Inst;
       public static GameMapBlockManager Inst { get { if (_Inst == null) { _Inst = new GameMapBlockManager(); } return _Inst; } }
       #endregion
       public GameMapBlockManager(){}

       RenderTexture rt;
       public void GenBlockThumb() 
       {
           GameObject cacheCamera = new GameObject("_GenBlockTextureCamera_");
           Camera camera = cacheCamera.AddComponent<Camera>();
           camera.orthographicSize = 1024;
           camera.orthographic = true;
           GameObject LightG = new GameObject();
           Light light = LightG.AddComponent<Light>();
           light.type = LightType.Directional;
           light.color = new Color(0.5f,0.5f,0.5f);
           LightG.transform.parent = camera.transform;
           Vector3 Origin = new Vector3(-1024+75*0.5f,1024 - 75*0.5f,150);
           Vector3 CurPos = Origin;
           foreach(KeyValuePair<int,MapStaticBlock> block in MapBlockData.Inst.Blocks)
           {
              GameObject objprefab = (GameObject)Resources.Load(block.Value.PrefabPath);
              GameObject blockPrefab = (GameObject)GameObject.Instantiate(objprefab);
              blockPrefab.transform.parent = cacheCamera.transform;
              blockPrefab.transform.rotation = Quaternion.identity;
              blockPrefab.transform.localScale = new Vector3(75,75,75);
              blockPrefab.transform.position =CurPos;
              CurPos.x += 75;
              if (CurPos.x >= 1024) 
              {
                  CurPos.y -= 75;
                  CurPos.x = 0;
              }
           }

           rt = new RenderTexture(2048,2048,1);
           camera.targetTexture = rt;
           camera.Render();
           this.sliceCameraTextureToThumb(camera.targetTexture);
           GameObject.DestroyImmediate(cacheCamera);
       }

       void sliceCameraTextureToThumb(RenderTexture targetTexture)
       {
           RenderTexture rt = targetTexture;
           if (rt == null) return;
           RenderTexture.active = rt;
           Texture2D tex = new Texture2D(75, 75, TextureFormat.ARGB32, false);
           tex.hideFlags = HideFlags.HideAndDontSave;
           Vector3 CurPos = Vector3.zero;
           foreach (KeyValuePair<int, MapStaticBlock> block in MapBlockData.Inst.Blocks)
           {
               tex.ReadPixels(new Rect(CurPos.x, CurPos.y, 75, 75),0,0); 
               tex.Apply();
               CurPos.x += 75;
               if (CurPos.x >= 2048) 
               {
                   CurPos.y += 75;
                   CurPos.x = 0;
               }
               byte[] bytes = tex.EncodeToPNG();
               FileStream fs = File.OpenWrite(Application.dataPath + "/Script/Core/GameMapMaker/Resources/BlockThumb/"+block.Key.ToString()+".png");
               fs.Write(bytes, 0, bytes.Length);
               fs.Close();
           }
           
       }
    }
